package com.yunji.storage.business;

import com.yunji.cabinet.enums.MessageEnum;
import com.yunji.common.base.BaseBusiness;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.common.utils.spring.SpringUtils;
import com.yunji.storage.dto.RollbackDTO;
import com.yunji.storage.dto.StorageOperateDTO;
import com.yunji.storage.pattern.*;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;


/**
 * @Project: yunji-coffee
 * @Package: com.yunji.storage.business
 * @Title: StockOperateBusiness
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-08-22 14:24:02      V1.0        HuaAo       新建类
 */
@Service
public class StorageInteractBusiness extends BaseBusiness {

    private static final Map<Integer, Class<? extends AbstractStorage>> BUSINESS_TYPE = new HashMap<>();

    static {
        // 调拨
        BUSINESS_TYPE.put(1, AllocateInteract.class);
        // 坏返
        BUSINESS_TYPE.put(2, RepairInteract.class);
        // 维修
        BUSINESS_TYPE.put(3, FixInteract.class);
        // 盘点
        BUSINESS_TYPE.put(4, StockTakeInteract.class);
        // 生产
        BUSINESS_TYPE.put(5, ProductInteract.class);
        // 采购
        BUSINESS_TYPE.put(6, PurchaseInteract.class);
        // 退货
        BUSINESS_TYPE.put(7, ReturnInteract.class);
        // 废品
        BUSINESS_TYPE.put(8, DiscardInteract.class);
        // 其他
        BUSINESS_TYPE.put(9, OtherInteract.class);
        // 销售
        BUSINESS_TYPE.put(11, SaleInteract.class);
        // 员工操作
        BUSINESS_TYPE.put(12, OperateInteract.class);
    }

    /**
     * 获取业务处理类
     *
     * @param businessType
     * @return
     */
    private AbstractStorage getInstance(Integer businessType) {
        Assert.isTrue(BUSINESS_TYPE.containsKey(businessType), MessageEnum.ERROR_00200010002.getInfo());
        Class<AbstractStorage> clazz = (Class<AbstractStorage>) BUSINESS_TYPE.get(businessType);
        AbstractStorage interact = SpringUtils.getBean(clazz);
        Assert.isTrue(BeanUtils.isNotNull(interact), MessageEnum.ERROR_00200010002.getInfo());
        return interact;
    }

    /**
     * 出入库库存操作
     *
     * @param operateDTO
     */
    public void operateStorage(StorageOperateDTO operateDTO) {
        Integer businessType = operateDTO.getBusinessType();
        AbstractStorage interact;
        try {
            interact = getInstance(businessType);
        } catch (Exception e) {
            logs.info("【库存管理】未知业务信息【{}】", operateDTO);
            return;
        }
        interact.operateStorage(operateDTO);
    }

    /**
     * 回滚库存
     *
     * @param dto
     */
    public void rollback(RollbackDTO dto) {
        Integer businessType = dto.getBusinessType();
        AbstractStorage interact;
        try {
            interact = getInstance(businessType);
        } catch (Exception e) {
            logs.info("【库存管理】未知业务信息【{}】", dto);
            return;
        }
        interact.rollback(dto);
    }

    /**
     * @param businessId   业务单ID
     * @param businessType 业务单类型
     * @Title updateTicket
     * @Desc 改变业务单状态
     * @Date 2024-08-29 16:36:24.480
     */
    public void updateTicket(Long businessId, Integer businessType) {
        AbstractStorage interact;
        try {
            interact = getInstance(businessType);
        } catch (Exception e) {
            logs.info("【库存管理】未知业务信息【{}】", businessType);
            return;
        }
        interact.updateTicket(businessId);
    }

    /**
     * @param businessType
     * @param businessCode
     * @Title checkTicket
     * @Desc 根据业务单号检验业务单是否存在
     * @Date 2024-09-12 16:58:04.821
     */
    public void checkTicket(Integer businessType, String businessCode) {
        AbstractStorage interact;
        try {
            interact = getInstance(businessType);
        } catch (Exception e) {
            logs.info("【库存管理】未知业务信息【{}】", businessType);
            return;
        }
        interact.checkTicket(businessCode);
    }
}
